package com.xiaoma.myaudience.biz.task;

import java.util.Map;

import org.apache.http.Header;
import org.apache.http.message.BasicHeader;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;

import com.xiaoma.myaudience.R;
import com.xiaoma.myaudience.biz.activity.LoginActivity;
import com.xiaoma.myaudience.biz.util.Constant;
import com.xiaoma.myaudience.util.HttpUtils;
import com.xiaoma.myaudience.util.Logger;
import com.xiaoma.myaudience.util.model.ModelUtils;
import com.xiaoma.myaudience.util.task.BaseDataAsyncTask;

/**
 * 参加活动信息.
 * 
 */
public class AddEventTask extends BaseDataAsyncTask<Map<String, String>, Void, Map<String, Object>>{
    private final static String TAG = "AddEventTask";
	private String mErrorMsg;
    
    public AddEventTask(Context context, int taskType, DataAsyncCallback callback) {
        super(context, taskType, callback);
    }

    @Override
    protected void onPostExecute(Map<String, Object> o) {
    	// TODO Auto-generated method stub
    	super.onPostExecute(o);
			addEventPorcess(o);
    }
    
    
	private void addEventPorcess(Map<String, Object> map) {
		if (!"0".equals(ModelUtils.getStringValue(map, "code"))) {
			mErrorMsg = ModelUtils.getStringValue(map, "errorMsg");
			showDialog(2);
		} else {
			
			//弹出信息
			mErrorMsg = ModelUtils.getStringValue(map, "errorMsg");
			showDialog(3);
		}
	}
    
    protected Dialog showDialog(int  id) {
   	 switch (id) {
        
        case 1: {
            AlertDialog.Builder builder = new Builder(mContext);
            builder.setPositiveButton(R.string.add_event_ok, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                	Intent intent = new Intent(mContext, LoginActivity.class);
                	mContext.startActivity(intent);
                }
            }).setNegativeButton(R.string.add_event_ok, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                	
                }
            }).setMessage(R.string.add_event_msg);
            return builder.create();
        }
        
        case 2: {
        	AlertDialog.Builder builder = new Builder(mContext);
        	builder.setNegativeButton(R.string.add_event_ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                	
                }
        	});
        	builder.setMessage("error");
        	return builder.create();
        }
        	
        case 3: {
        	AlertDialog.Builder builder = new Builder(mContext);
        	builder.setMessage(R.string.login_please);
        	builder.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						Intent intent= new Intent(mContext, LoginActivity.class);
						mContext.startActivity(intent);
					}
        	});
        	return builder.create();
        }
    }
    return null;
   }
	
	
    @Override
    protected Map<String, Object> doInBackground(Map<String, String>... params) {  
    	String eventId = null;
		Map<String, String> cookie = null;
    	if (params != null) {
    		cookie = params[0];
		}
    	Map<String, Object> result = null;
        try {            
        	String cookiestr = Constant.LOGIN_COOKIE1+"="+cookie.get(Constant.LOGIN_COOKIE1)+";"+Constant.LOGIN_COOKIE2+"="+cookie.get(Constant.LOGIN_COOKIE2);
			Header[] headers = new BasicHeader[2];//("X-Online-Host",  format_CMWAP_ServerName(strPath));  
			headers[0]=new BasicHeader("Cookie", cookiestr);
			headers[1]=new BasicHeader("Accept-Language","zh-CN");
			eventId = cookie.get("url");
			String str = HttpUtils.getHttpStringResult(getHttpClient(), String.format(Constant.URL_ADD_EVENT, eventId),
					null, headers, HttpUtils.GET, "GBK");
			
        	JSONObject jsonObject = new JSONObject(str);
        	if (jsonObject != null) {
    			Map<String, Object> objMap = ModelUtils.json2Map(jsonObject);
    			result = objMap;
			}
        } catch (Exception e) {
            Logger.e(TAG, "doInBackground error!!!", e);
        } finally {
            closeHttpClient();
        }
        
        return result;
    }
}
